package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Setmeal;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.SetmealService;
import com.atguigu.util.QiniuUtils;
import com.atguigu.util.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/Setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;


    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;
    }

    @PostMapping("/findAllGroup")
    public Result findAllGroup(){
        try {
            List<TravelGroup> list = setmealService.findAllGroup();
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }

    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {

        byte[] bytes = imgFile.getBytes();
        String filename = imgFile.getOriginalFilename();
        String s = UUID.randomUUID().toString();
        String newFileName = s + filename;

        try {
            QiniuUtils.upload2Qiniu(bytes,newFileName);
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal , Integer[] travelgroupIds){
        try {
            setmealService.add(setmeal,travelgroupIds);
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result delete(Integer id){

        try {
            int i = setmealService.getGroupSetmealById(id);
            if(i>0){
                return new Result(false,"有关联数据存在，删除失败");
            }else{
                setmealService.delete(id);
                return new Result(true,"删除套餐游成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除套餐游失败");
        }
    }

}
